package com.wx.mall.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.wx.mall.goods.dto.AppaiseDto;
import com.wx.mall.goods.entity.Appaise;
import com.wx.mall.goods.mapper.AppaiseMapper;
import com.wx.mall.goods.service.IAppaiseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author j
 * @since 2020-05-04
 */
@Service
public class AppaiseServiceImpl extends ServiceImpl<AppaiseMapper, Appaise> implements IAppaiseService {


    @Override
    public List<Appaise> getList(AppaiseDto dto) {
        PageHelper.startPage(dto.getPageNum(),dto.getPageSize());

//        获取查询条件

        QueryWrapper<Appaise> wrapper=new QueryWrapper<>();
        if (!dto.getSpuName().equals("")){
            wrapper.like("spu_name",dto.getSpuName());
        };
        if (!dto.getUserName().equals("")){
            wrapper.like("user_name",dto.getUserName());
        };


        switch (dto.getType()){

            case 0:
                wrapper.orderByAsc("user_name");
                break;
            case 1:
                wrapper.orderByAsc("spu_name");
                break;

        };

        List<Appaise> list=this.list(wrapper);

        return list;
    };


    @Override
    public int updateAll(List<Integer> ids, int type) {
QueryWrapper<Appaise> wrapper=new QueryWrapper<>();
boolean suceess=false;
//1删除
        if(type==1){
            for (Integer id:ids
                 ) {
               suceess=removeById(id);
            }
        };
//        2显示
        if(type==2){
            for (Integer id:ids
            ) {
                Appaise appaise=new Appaise();
                appaise.setId(id);
                appaise.setIsShow(1);
                suceess=updateById(appaise);
            }
        };
//        3隐藏
        if(type==3){
            for (Integer id:ids
            ) {
                Appaise appaise=new Appaise();
                appaise.setId(id);
                appaise.setIsShow(0);
                suceess=updateById(appaise);
            }
        };

//1成功0失败
        return suceess?1:0;
    };


    @Override
    public List<Appaise> getListByUseId(Integer pageNum,Integer pageSize,Integer spuId) {

        PageHelper.startPage(pageNum,pageSize);
        QueryWrapper<Appaise> wrapper=new QueryWrapper<>();
        wrapper.eq("spu_id",spuId).eq("is_show",1);

        List<Appaise> list =this.list(wrapper);

        return list;
    }
}
